package com.shi.servlet;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shi.pojo.Exercises;
import com.shi.pojo.User;
import com.shi.service.UserService;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;

public class UserServlet extends BaseServlet {

	UserService service = new UserService();
	public String addExercise(HttpServletRequest request, HttpServletResponse response){
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("user");
			if(user == null){
				return "r:/login.jsp";
			}
	        String id = CommonUtils.uuid();
	        String title = request.getParameter("title");
			String type = request.getParameter("type");
			String answer = request.getParameter("answer");
			String uploadUserId= user.getUserId();
			String A = request.getParameter("A");
			String B = request.getParameter("B");
			String C = request.getParameter("C");
			String D = request.getParameter("D");
			service.addExercise(id, title,type,A,B,C,D,answer,uploadUserId);
	        return "index.jsp";
	}


	public String deleteExercise(HttpServletRequest request, HttpServletResponse response){
	       String id = request.getParameter("id");
	       service.deleteExercise(id);
	       return "ExciseListView.jsp";
	}
	public String changeExercise(HttpServletRequest request, HttpServletResponse response){
	        String id = request.getParameter("id");
			String answer = request.getParameter("answer");
			service.changeExercise(id, answer);
	        return "ExciseListView.jsp";
	 }
	
	public String regist(HttpServletRequest request, HttpServletResponse response){
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String repassword = request.getParameter("repassword");
		String email = request.getParameter("email");
		String roleId = CommonUtils.uuid();
		String userId = CommonUtils.uuid();
		try {
			service.regist(username,password,repassword,email,roleId,userId);
		} catch (Exception e) {
			User form = new User();
			form.setUserName(username);
			form.setEmail(email);
			request.setAttribute("msg",e.getMessage());
			request.setAttribute("form",form);
			return "regist.jsp";
		}
		return "login.jsp";
	}
	public String login(HttpServletRequest request, HttpServletResponse response)  {
		HttpSession session = request.getSession();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		try {
			User user = service.login(username,password);
			session.setAttribute("user",user);
			return "r:/main.jsp";
		} catch (Exception e) {
			User form = new User();
			form.setUserName(username);
			session.setAttribute("msg",e.getMessage());
			session.setAttribute("form",form);
			return "r:/login.jsp";
		}
	}
	public String checkAnswer(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user == null){
			return "r:/login.jsp";
		}
		List<Exercises> exercises = new ArrayList<Exercises>();
		String one = request.getParameter("one");
		String oneAnswer = request.getParameter("oneAnswer");
		String oneTitle = request.getParameter("oneTitle");
		Exercises exercise1 = new Exercises();
		exercise1.setExercisesId(one);
		exercise1.setAnswer(oneAnswer);
		exercise1.setTitle(oneTitle);
		exercises.add(exercise1);
		String two = request.getParameter("two");
		String twoAnswer = request.getParameter("twoAnswer");
		String twoTitle = request.getParameter("twoTitle");
		Exercises exercise2 = new Exercises();
		exercise2.setExercisesId(two);
		exercise2.setAnswer(twoAnswer);
		exercise2.setTitle(twoTitle);
		exercises.add(exercise2);
		String three = request.getParameter("three");
		String threeAnswer = request.getParameter("threeAnswer");
		String threeTitle = request.getParameter("threeTitle");
		Exercises exercise3 = new Exercises();
		exercise3.setExercisesId(three);
		exercise3.setAnswer(threeAnswer);
		exercise3.setTitle(threeTitle);
		exercises.add(exercise3);
		String four = request.getParameter("four");
		String fourAnswer = request.getParameter("fourAnswer");
		String fourTitle = request.getParameter("fourTitle");
		Exercises exercise4 = new Exercises();
		exercise4.setExercisesId(four);
		exercise4.setAnswer(fourAnswer);
		exercise4.setTitle(fourTitle);
		exercises.add(exercise4);
		String five = request.getParameter("five");
		String fiveAnswer = request.getParameter("fiveAnswer");
		String fiveTitle = request.getParameter("fiveTitle");
		Exercises exercise5 = new Exercises();
		exercise5.setExercisesId(five);
		exercise5.setAnswer(fiveAnswer);
		exercise5.setTitle(fiveTitle);
		exercises.add(exercise5);
		String six = request.getParameter("six");
		String sixAnswer = request.getParameter("sixAnswer");
		String sixTitle = request.getParameter("sixTitle");
		Exercises exercise6 = new Exercises();
		exercise6.setExercisesId(six);
		exercise6.setAnswer(sixAnswer);
		exercise6.setTitle(sixTitle);
		exercises.add(exercise6);
		String seven = request.getParameter("seven");
		String sevenAnswer = request.getParameter("sevenAnswer");
		String sevenTitle = request.getParameter("sevenTitle");
		Exercises exercise7 = new Exercises();
		exercise7.setExercisesId(seven);
		exercise7.setAnswer(sevenAnswer);
		exercise7.setTitle(sevenTitle);
		exercises.add(exercise7);
		String eight = request.getParameter("eight");
		String eightAnswer = request.getParameter("eightAnswer");
		String eightTitle = request.getParameter("eightTitle");
		Exercises exercise8 = new Exercises();
		exercise8.setExercisesId(eight);
		exercise8.setAnswer(eightAnswer);
		exercise8.setTitle(eightTitle);
		exercises.add(exercise8);
		String nine = request.getParameter("nine");
		String nineAnswer = request.getParameter("nineAnswer");
		String nineTitle = request.getParameter("nineTitle");
		Exercises exercise9 = new Exercises();
		exercise9.setExercisesId(nine);
		exercise9.setAnswer(nineAnswer);
		exercise9.setTitle(nineTitle);
		exercises.add(exercise9);
		String ten = request.getParameter("ten");
		String tenAnswer = request.getParameter("tenAnswer");
		String tenTitle = request.getParameter("tenTitle");
		Exercises exercise10 = new Exercises();
		exercise10.setExercisesId(ten);
		exercise10.setAnswer(tenAnswer);
		exercise10.setTitle(tenTitle);
		exercises.add(exercise10);
		
		int score = service.checkAnswer(exercises, user.getUserId());
		request.setAttribute("score", score);
        return "score.jsp";
	}
	public String doingExercises(HttpServletRequest request, HttpServletResponse response)  {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user == null){
			return "r:/login.jsp";
		}
		List<Exercises> exercises = service.doiongExercises();
		request.setAttribute("exercises", exercises);
		return "f:/doingexercises.jsp";
	}
	public String findWrongExerciseByUser(HttpServletRequest request, HttpServletResponse response)  {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user == null){
			return "r:/login.jsp";
		}
		List<Exercises> exercises = service.findWrongExerciseByUser(user.getUserId());
		request.setAttribute("exercises", exercises);
		return "f:/mywrongexercises.jsp";
	}
	public String findWrongExerciseById(HttpServletRequest request, HttpServletResponse response)  {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user == null){
			return "r:/login.jsp";
		}
		String exercisesId = request.getParameter("exerciseId");
		Exercises exercises = service.findWrongExerciseByExerciseId(exercisesId,user.getUserId());
		request.setAttribute("exercise", exercises);
		return "f:/wrongexercise.jsp";
	}
	public void collection(HttpServletRequest request, HttpServletResponse response)  {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		String exercisesId = request.getParameter("exerciseId");
		int i = service.addExercise(exercisesId,user.getUserId());
		try {
			if(i == 1){
				response.getWriter().print(1);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public String findMyExercise(HttpServletRequest request, HttpServletResponse response)  {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user == null){
			return "r:/login.jsp";
		}
		List<Exercises> exercises  = service.findMyExercise(user.getUserId());
		request.setAttribute("exercises", exercises);
		return "mycollection.jsp";
	}
	public void uncollection(HttpServletRequest request, HttpServletResponse response)  {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		String exercisesId = request.getParameter("exerciseId");
		int i = service.removeExercise(exercisesId,user.getUserId());
		try {
			if(i == 1){
				response.getWriter().print(1);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
